package com.rc.saas.tenant.mq;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rc.saas.tenant.common.mq.MqConfig;
import com.rc.saas.tenant.model.system.RcSetting;
import com.rc.saas.tenant.service.channel.RcChannelApplyService;
import com.rc.saas.tenant.service.enterprise.EnterpriseMemberApplyService;
import com.rc.saas.tenant.service.enterprise.EnterpriseSubmemberApplyService;
import com.rc.saas.tenant.service.invoice.InvoiceApplyService;
import com.rc.saas.tenant.service.operator.RcOperatorService;
import com.rc.saas.tenant.service.order.OrderRefundApplyService;
import com.rc.saas.tenant.service.system.RcSettingService;
import com.rc.saas.tenant.service.transfer.MemberRefundApplyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 */
@Component
public class PlatformApplyListener{
    private static Logger logger = LogManager.getLogger(PlatformApplyListener.class);

    @Autowired
    private RcSettingService settingService;

    @Autowired
    private RcChannelApplyService channelApplyService;

    @Autowired
    private InvoiceApplyService invoiceApplyService;

    @Autowired
    private MemberRefundApplyService memberRefundApplyService;

    @Autowired
    private OrderRefundApplyService orderRefundApplyService;

    @Autowired
    private EnterpriseMemberApplyService enterpriseMemberApplyService;

    @Autowired
    private RcOperatorService operatorService;

    @Autowired
    private EnterpriseSubmemberApplyService enterpriseSubmemberApplyService;


    @RabbitListener(queues = {MqConfig.platform_apply})
    @RabbitHandler
    public void processOpen2wxNotifyStartChargeResult(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("PlatformApplyListener 收到了:{}", msg);


        try {
            //alibaba的解析不完整
            PlatformApply log = JSON.parseObject(msg,PlatformApply.class);

            RcSetting setting = settingService.selectSetting();
            //没有设置,默认不处理
            if (setting == null) {
                return;
            }
            //rc_setting 获取配置分析流程走向
            if(log.getChannelApplyId()!=null){

                //渠道商申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
                if ("1".equals(setting.getChannelApplyType())) {
                    channelApplyService.updateChannelApplyForAudit(log.getChannelApplyId(), "1", null);
                }
            }
            if(log.getInvoiceApplyId()!=null){
                //发票申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
                if ("1".equals(setting.getInvoiceApplyType())) {
                    invoiceApplyService.saveReview(log.getInvoiceApplyId(), "1");
                }
            }
            if(log.getOrderRefundApplyId()!=null){
                //订单退款申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
                if ("1".equals(setting.getMemberRefundApplyType())) {
                    orderRefundApplyService.updateStatus(log.getInvoiceApplyId(), "1","自动退款");
                }

            }
            if(log.getRefundApplyId()!=null){
                //提现申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
            }
            if(log.getEnterpriseApplyId()!=null){
                //企业（认证）申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
                if ("1".equals(setting.getEnterpriseApplyType())) {
                    enterpriseMemberApplyService.updateEnterpriseApplyForAudit(log.getEnterpriseApplyId(), "1", null);
                }
            }
//            if(log.getOperatorApplyId()!=null){
//                //加盟商申请
//                //0.手动，走手动流程，审核后走通过流程
//                //1.自动，走通过流程
//                if ("1".equals(setting.getOperatorApplyType())) {
//                    operatorService.updateOperatorApplyForAudit(log.getOperatorApplyId(), null,"1", null);
//                }
//            }
            if (log.getSubmemberApplyId() != null) {
                //企业成员申请
                //0.手动，走手动流程，审核后走通过流程
                //1.自动，走通过流程
                if ("1".equals(setting.getEnterpriseMemberApplyType())) {
                    enterpriseSubmemberApplyService.updateSubmemberApplyForAudit(log.getSubmemberApplyId(), "1", null);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            logger.error("market_activity_result 错误", e);
        }
    }
    public static  class PlatformApply{
        private Long channelApplyId;
        private Long operatorApplyId;
        private Long enterpriseApplyId;
        private Long orderRefundApplyId;
        private Long invoiceApplyId;
        private Long refundApplyId;
        private Long submemberApplyId;

        public Long getChannelApplyId() {
            return channelApplyId;
        }

        public void setChannelApplyId(Long channelApplyId) {
            this.channelApplyId = channelApplyId;
        }

        public Long getOperatorApplyId() {
            return operatorApplyId;
        }

        public void setOperatorApplyId(Long operatorApplyId) {
            this.operatorApplyId = operatorApplyId;
        }

        public Long getEnterpriseApplyId() {
            return enterpriseApplyId;
        }

        public void setEnterpriseApplyId(Long enterpriseApplyId) {
            this.enterpriseApplyId = enterpriseApplyId;
        }

        public Long getOrderRefundApplyId() {
            return orderRefundApplyId;
        }

        public void setOrderRefundApplyId(Long orderRefundApplyId) {
            this.orderRefundApplyId = orderRefundApplyId;
        }

        public Long getInvoiceApplyId() {
            return invoiceApplyId;
        }

        public void setInvoiceApplyId(Long invoiceApplyId) {
            this.invoiceApplyId = invoiceApplyId;
        }

        public Long getRefundApplyId() {
            return refundApplyId;
        }

        public void setRefundApplyId(Long refundApplyId) {
            this.refundApplyId = refundApplyId;
        }

        public Long getSubmemberApplyId() {
            return submemberApplyId;
        }

        public void setSubmemberApplyId(Long submemberApplyId) {
            this.submemberApplyId = submemberApplyId;
        }
    }
}
